package com.kcht.test.rfid;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 测试报告分析器，结果个数分析，结果顺序分析
 * */
@Slf4j
@Component
public class ReportAnalyzer {
    @Value("#{'${service.log.match-order}'.split(',')}")
    private List<String> matchOrder;

    /**
     * 分析报文的时间忽略不计
     * */
    public boolean analyzer(List<String> targetList){
        this.logTarget(targetList);
        if (this.matchOrder != null) {
            for (int i = 0; i < this.matchOrder.size(); i++) {
                try {
                    if (!targetList.get(i).matches("^.*\r*" + this.matchOrder.get(i) + ".*\r*$")) {
                        log.info("不匹配--->服务异常");
                        return false;
                    }
                } catch (IndexOutOfBoundsException e) {
                    log.info("缺少报文--->服务异常");
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 打印报告信息
     * */
    private void logTarget(List<String> targetList){
        for (String s : targetList) {
            log.info("收集到报文--->{}",s);
        }
    }
}
